//
//  MTFriendViewRequestVerifyController.swift
//  MT
//
//  Created by ooops on 2020/5/6.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory
import YPNavigationBarTransition

extension MTFriendViewRequestVerifyController: MTFriendViewRequestVerifyCellDelegate, MTRequestVerifyDelegate {
    
    func confrimInvitation(userName: String) {
        self.tableView.reloadData()
    }
    
    func rejectInvitation() {
        self.tableView.reloadData()
    }
    
    /// 同意申请信息
    /// - Parameters:
    ///   - userName: 同意的用户信息
    ///   - eventType: 事件类型1同意加好友，2同意加俱乐部
    ///   - pushId: 群组ID
    func acceptInvitation(userName: String, eventType: Int, pushId: String?) {
        let requestParam: [String : Any] = ["eventType": eventType, "pushId": pushId ?? "", "userName": userName]
        
        UserCenterApiTool.notify(provider: userProvider, requestParam: requestParam, complete: { (model) in
            debugPrint("------------------------", model?.code ?? 0)
            if model?.code == 200, eventType == 1 {
                NotificationCenter.default.post(name: GlobalNotificationName.didBecomeFriend.name, object: nil, userInfo: ["userName": userName])
            }
        }) { (error) in
            
        }
    }
}

extension MTFriendViewRequestVerifyController: UITableViewDelegate, UITableViewDataSource {
    
    //MARK: UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return verifications.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let v = verifications[indexPath.row]
        
        switch v.type {
//        case .single:
//            let cell: MTFriendViewRequestVerifyCell = tableView.dequeueReusableCell(withIdentifier: MTFriendViewRequestVerifyCell.reuseIdentity(), for: indexPath) as! MTFriendViewRequestVerifyCell
//            cell.delegate = self
//            cell.verificationInfo = v
//            return cell
        case .group:
            let cell: MTClubViewRequestVerifyCell = tableView.dequeueReusableCell(withIdentifier: MTClubViewRequestVerifyCell.reuseIdentity(), for: indexPath) as! MTClubViewRequestVerifyCell
            cell.delegate = self
            cell.verificationInfo = v
            return cell
        default:
            let cell: MTFriendViewRequestVerifyCell = tableView.dequeueReusableCell(withIdentifier: MTFriendViewRequestVerifyCell.reuseIdentity(), for: indexPath) as! MTFriendViewRequestVerifyCell
            cell.delegate = self
            cell.verificationInfo = v
            return cell
        }
    }
    
    //MARK: UITableViewDelegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return UITableView.automaticDimension
//    }
//
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 6
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        return 6
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
    
}

class MTFriendViewRequestVerifyController: BaseViewController {
    
    private var verifyType: MTRequestVerifyType = .single
    
    let userProvider: MTProvider = MTProvider<UserCenterApi>()
    
    private var verifications: [JCVerificationInfo] = []
    
    private func loadRequestData() {
        
        verifications = JCVerificationInfoDB.shareInstance.quaryData(verType: verifyType.rawValue)
        
        self.tableView.reloadData()
        
    }
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: view.bounds, style: .grouped)
        table.delegate = self
        table.dataSource = self
        table.register(MTFriendViewRequestVerifyCell.self, forCellReuseIdentifier: MTFriendViewRequestVerifyCell.reuseIdentity())
        table.register(MTClubViewRequestVerifyCell.self, forCellReuseIdentifier: MTClubViewRequestVerifyCell.reuseIdentity())
        table.separatorStyle = .none
        table.tableFooterView = UIView()
        table.estimatedRowHeight = 44
        table.rowHeight = UITableView.automaticDimension
        table.sectionHeaderHeight = 0
        table.sectionFooterHeight = 0
        table.emptySetView = ViewFactory.emptySetView(nil, nil, { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.loadRequestData()
        })
        return table
    }()
    
    
    convenience init(friendRequestType type: MTRequestVerifyType) {
        self.init()
        self.verifyType = type
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "待处理请求"
        view.addSubview(tableView)
        tableView.panGestureRecognizer.require(toFail: self.navigationController!.interactivePopGestureRecognizer!)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        loadRequestData()
    }
}


extension MTFriendViewRequestVerifyController {
    
    override func yp_navigtionBarConfiguration() -> YPNavigationBarConfigurations {
        return [.backgroundStyleColor, .backgroundStyleOpaque, .showShadowImage]
    }
}
